#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    bool wordPattern(string pattern, string s) {
        int left = 0, right = 0;
        vector<string> tmp;
        while (right < s.size())
        {
            while (right < s.size() && s[right] != ' ') right++;
            string str = s.substr(left, right - left);
            tmp.push_back(str);
            left = ++right;
        }

        if (tmp.size() != pattern.size()) return false;
        unordered_map<char, string> p_to_s;
        unordered_map<string, char> s_to_p;

        int n = pattern.size();
        for (int i = 0; i < n; i++)
        {
            if (p_to_s[pattern[i]] == "") p_to_s[pattern[i]] = tmp[i];
            else
            {
                if (p_to_s[pattern[i]] != tmp[i]) return false;
            }
        }
        for (int i = 0; i < n; i++)
        {
            if (!s_to_p[tmp[i]]) s_to_p[tmp[i]] = pattern[i];
            else
            {
                if (s_to_p[tmp[i]] != pattern[i]) return false;
            }
        }
        return true;

    }
};